package mysql

import (
	"hema-fresh/hemarpc/hemarpc"

	"gorm.io/gorm"
)

type Good struct {
	gorm.Model
	GoodName  string  `gorm:"type:varchar(30);not null;comment:'商品名称'"`
	GoodType  string  `gorm:"type:varchar(50);not null;comment:'商品类型'"`
	GoodPrice float64 `gorm:"type:decimal(10,1);not null;comment:'商品价格'"`
	GoodNum   int64   `gorm:"type:int(11);not null;comment:'商品数量'"`
	ClassId   int64   `gorm:"type:int(11);not null;comment:'分类id'"`
}

func (g *Good) GoodAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}
func (g *Good) GoodList(db *gorm.DB, in *hemarpc.GoodListRequest) ([]*hemarpc.GoodList, error) {
	var list []*hemarpc.GoodList
	err2 := db.Debug().Model(&Good{}).
		Select("goods.id as good_id", "good_name", "good_type", "good_price", "good_num").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}

func (g *Good) FindGoodsByName(db *gorm.DB, name string) error {
	return db.Where("good_name=?", name).First(g).Error
}
